package win.smartown.app.weather;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import win.smartown.app.R;
import win.smartown.app.base.BaseFragment;
import win.smartown.app.base.SuperActivity;
import win.smartown.app.util.DimensionUtil;
import win.smartown.app.weather.entity.Location;
import win.smartown.app.weather.entity.MultipleData;
import win.smartown.app.weather.entity.Utils;
import win.smartown.app.weather.entity.forecast.Daily;
import win.smartown.app.weather.entity.realTime.RealTime;
import win.smartown.easyAdapter.EasyAdapter;
import win.smartown.easyAdapter.EasyViewHolder;

/**
 * Created by smartown on 2017/5/8 14:01.
 * Desc:
 */
public class WeatherFragment extends BaseFragment implements Contract.View {

    private SwipeRefreshLayout swipeRefreshLayout;

    private Location location;
    private Contract.Presenter presenter;
    private RecyclerView.Adapter adapter;

    public static WeatherFragment newInstance(Location location) {
        Bundle args = new Bundle();
        args.putParcelable("location", location);
        WeatherFragment fragment = new WeatherFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        setContentView(R.layout.fragment_weather);
        swipeRefreshLayout = findView(R.id.weather_refresh);
        RecyclerView recyclerView = findView(R.id.weather_content);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(adapter);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.refresh();
            }
        });
        presenter.refresh();
    }

    private void init() {
        location = getArguments().getParcelable("location");
        presenter = new Contract.Presenter(this, location);
        adapter = new EasyAdapter(getActivity()) {

            @Override
            public int getItemViewType(int position) {
                return presenter.getDataList().get(position).getType();
            }

            @Override
            public int getItemLayout(int viewType) {
                switch (viewType) {
                    case 1:
                        return R.layout.layout_weather_header;
                }
                return R.layout.item_daily_weather;
            }

            @Override
            public void onBindViewHolder(EasyViewHolder holder, int position) {
                final MultipleData data = presenter.getDataList().get(position);
                final int screenWidth = DimensionUtil.getScreenWidth(getResources());
                switch (data.getType()) {
                    case 1:
                        holder.itemView.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, screenWidth));
                        RealTime realTime = (RealTime) data.getObject();
                        holder.getTextView(R.id.weather_temp).setText(Utils.formatTemp(realTime.getTemperature()));
                        holder.getTextView(R.id.weather_location).setText(location.getDistrict());
                        holder.getTextView(R.id.weather_skycon).setText(Utils.getSkycon(realTime.getSkycon()));
                        holder.getTextView(R.id.weather_wind).setText(Utils.getWindDirection(realTime.getWind().getDirection()) + "风");
                        holder.getTextView(R.id.weather_wind_level).setText(Utils.getWindLevel(realTime.getWind().getSpeed()));
                        holder.getTextView(R.id.weather_humidity).setText(Utils.formatHumidity(realTime.getHumidity()));
                        holder.getTextView(R.id.weather_body_temp).setText(Utils.formatTemp(realTime.getTemperature()));
                        break;
                    case 2:
                        holder.itemView.setLayoutParams(new ViewGroup.LayoutParams(screenWidth, DimensionUtil.dip2px(getResources(), 75)));
                        final Daily.DailyInfo info = (Daily.DailyInfo) data.getObject();
                        holder.getTextView(R.id.daily_weather_date).setText(info.getAqi().getDate());
                        holder.getTextView(R.id.daily_weather_aqi).setText(info.getAqi().getAvg() + "");
                        holder.getTextView(R.id.daily_weather_skycon).setText(Utils.getSkycon(info.getSkycon().getValue()));
                        holder.getTextView(R.id.daily_weather_temp).setText(Utils.formatTemp(info.getTemperature().getMin()) + "/" + Utils.formatTemp(info.getTemperature().getMax()));
                        break;
                }
            }

            @Override
            public int getItemCount() {
                return presenter.getDataList().size();
            }
        };
    }

    @Override
    public void startLoading() {
        swipeRefreshLayout.setRefreshing(true);
    }

    @Override
    public void finishLoading() {
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public SuperActivity getSuperActivity() {
        return super.getSuperActivity();
    }

    @Override
    public void onDataChanged() {
        adapter.notifyDataSetChanged();
    }
}
